package com.example.common_base.net;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Converter.Factory;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 数据转换
 */
public class HttpResultConverter extends Factory {
    private static final String TAG = "HttpResultConverter";
    private final Gson mGson = new Gson();

    public static HttpResultConverter create() {
        return new HttpResultConverter();
    }

    private HttpResultConverter() {
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type mType, Annotation[] annotations, Retrofit retrofit) {
        return new BaseResponseBodyConverter(mType);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type mType, Annotation[] parameterAnnotations, Annotation[] methodAnnotations,
                                                          Retrofit retrofit) {
        return GsonConverterFactory.create().requestBodyConverter(mType, parameterAnnotations, methodAnnotations, retrofit);
    }

    private class BaseResponseBodyConverter<T> implements Converter<ResponseBody, T> {
        private Type mType;
        private static final String SERVICE_ERROR = "请求服务器异常";
        private static final String REQUEST_SUCCESS = "200";

        public BaseResponseBodyConverter(Type mType) {
            this.mType = mType;
        }

        @Override
        public T convert(ResponseBody response) {
            Object var6 = null;
            try {
                String strResponse = response.string();
//                MyLogger.dLog().e("返回数据:   \n" + strResponse);
                if (TextUtils.isEmpty(strResponse)) {
                    throw new HttpException(SERVICE_ERROR);
                } else {
                    HttpResponseData httpResponse = mGson.fromJson(strResponse, HttpResponseData.class);
                    String code = httpResponse.getCode();
                    if (code.equals(REQUEST_SUCCESS)) {
                        String json = mGson.toJson(httpResponse.getData());
                        try {
                            var6 = mGson.fromJson(json, this.mType);
                        } catch (Exception e) {
                            Log.e("tag", "convert: " + e.getMessage());
                        }
                    } else if (code.equals("401")) {
                        throw new ApiException(code, httpResponse.getMessage());
                    } else {

                        Log.e("tag", "net=="+httpResponse.getMessage());
                        throw new HttpException(httpResponse.getMessage());
                    }
                }
            } catch (IOException var10) {
                throw new HttpException(var10.getMessage());
            } finally {
                response.close();
            }
            return (T) var6;
        }
    }
}
